package com.zhang.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhang.entity.BaseCategory1;
import com.zhang.entity.BaseCategory2;
import com.zhang.entity.BaseCategory3;
import com.zhang.result.RetVal;
import com.zhang.service.BaseCategory1Service;
import com.zhang.service.BaseCategory2Service;
import com.zhang.service.BaseCategory3Service;
import com.zhang.service.BaseCategoryViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 一级分类表 前端控制器
 * </p>
 *
 * @author zbw
 * @since 2022-04-03
 */
@RestController
@RequestMapping("/product")
//@CrossOrigin
public class CategoryController {

    @Autowired
    private BaseCategory1Service category1Service;
    @Autowired
    private BaseCategory2Service category2Service;
    @Autowired
    private BaseCategory3Service category3Service;
    @Autowired
    private BaseCategoryViewService categoryViewService;

    //1.查询一级商品分类 http://127.0.0.1:8000/product/getCategory1
    @GetMapping("getCategory1")
    public RetVal getCategory1(){
        List<BaseCategory1> category1List = category1Service.list(null);
        return RetVal.ok(category1List);
    }

    //2.查询二级商品分类,要用到一级商品分类的id：http://127.0.0.1:8000/product/getCategory2/2
    @GetMapping("getCategory2/{category1Id}")
    public RetVal getCategory2(@PathVariable Long category1Id){
        QueryWrapper<BaseCategory2> wrapper = new QueryWrapper<>();
        wrapper.eq("category1_id",category1Id);
        List<BaseCategory2> category2List = category2Service.list(wrapper);
        return RetVal.ok(category2List);
    }

    //3.查询第三级商品分类，要用到二级商品分类的id：http://127.0.0.1:8000/product/getCategory3/13
    @GetMapping("getCategory3/{category2Id}")
    public RetVal getCategory3(@PathVariable Long category2Id){
        QueryWrapper<BaseCategory3> wrapper = new QueryWrapper<>();
        wrapper.eq("category2_id",category2Id);
        List<BaseCategory3> category3List = category3Service.list(wrapper);
        return RetVal.ok(category3List);
    }

    //4.查询商品首页商品分类信息
    @GetMapping("getIndexCategory")
    public RetVal getIndexCategory(){
        List<JSONObject> categoryViewList=categoryViewService.getIndexCategory();
        return RetVal.ok(categoryViewList);
    }
}

